import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class dbRetriever {
	public static ArrayList<connectionObj> getConnectionStrings () {
		try {
			ArrayList<connectionObj> cn = new ArrayList<connectionObj>();
			try {
				Class.forName("org.sqlite.JDBC");
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
				return null;
			}
			Connection con = DriverManager.getConnection("jdbc:sqlite:odbcConnectionCheckerV2.db");
			Statement stat = con.createStatement();
			ResultSet rst = stat.executeQuery("select * from dbConnection");
			ResultSet rst2;
			connectionObj temp_cn;
			connectionAction temp_act;
			temp_cn = new connectionObj();
			int onClick, onOk, onFail, type;
			while (rst.next()) {
				temp_cn = new connectionObj();
				temp_cn.setConnectionID(rst.getLong("ConnectionID"));
				temp_cn.setConnectionName(rst.getString("ConnectionName"));
				temp_cn.setConnectionString(rst.getString("ConnectionString"));
				rst2 = stat.executeQuery("SELECT * FROM dbaction WHERE ConnectionID = " + temp_cn.getConnectionID());
				while (rst.next()) { 
					temp_act = new connectionAction();
					temp_act.setActionID(rst2.getLong("ActionID"));
					temp_act.setActionCommand(rst2.getString("ActionCommand"));
					type = 0;
					onClick = rst2.getInt("onClick");
					onOk = rst2.getInt("onOK");
					onFail = rst2.getInt("onFail");
					if (onClick == 1) {
						type+=connectionAction.c_onClik; 
					}
					if (onOk == 1) { 
						type+=connectionAction.c_onOk;
					}
					if (onFail == 1) { 
						type+=connectionAction.c_onFail;
					}
					temp_act.setActionType(type);
					temp_cn.addAction(temp_act);
				}
				cn.add(temp_cn);
			}
			rst.close();
			con.close();
			return cn ;
			
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}	
	
	public static String checkConnection(String jdbc) {
		try {
			Connection con = DriverManager.getConnection(jdbc);
			con.close();
		} catch (SQLException e) {
			return e.getMessage();
		}
		return "true";
	}
}

